package yl.hs.bmipfull.service.vehiclemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.vehiclemanage.IStorageTankDao;
import yl.hs.bmipfull.daos.vehiclemanage.ITruckAddFuelRecordDao;
import yl.hs.bmipfull.pojo.vehiclemanage.StorageTankEntity;
import yl.hs.bmipfull.pojo.vehiclemanage.TruckAddFuelRecordEntity;
import yl.hs.bmipfull.service.vehiclemanage.ITruckAddFuelRecordService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 作用:  车辆加油记录表服务层接口实现<br/>
 * 创建时间: 2021年06月17日 10时16分09秒 <br/>
 *
 * @author 谢东
 */
@Service
public class TruckAddFuelRecordServiceImpl extends VehicleBaseService implements ITruckAddFuelRecordService {

    @Autowired
    ITruckAddFuelRecordDao dao;

    @Autowired
    IStorageTankDao tankDao;

    @Override
    public List<TruckAddFuelRecordEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(TruckAddFuelRecordEntity.class);var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),TruckAddFuelRecordEntity.class);
        return dao.queryPage(pageQuery.getQuery(),filters);
    }

    @Override
    public PageResult<TruckAddFuelRecordEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(TruckAddFuelRecordEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<TruckAddFuelRecordEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),TruckAddFuelRecordEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public TruckAddFuelRecordEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveTruckAddFuelRecord(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        TruckAddFuelRecordEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new TruckAddFuelRecordEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(TruckAddFuelRecordEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入车辆加油记录表信息", Tl.toJson(entity), TruckAddFuelRecordEntity.class);
                if(entity.getTankNoSn()!=null && !entity.getTankNoSn().equals("")){
                    var bySN = tankDao.findBySN(entity.getTankNoSn());
                        BigDecimal subtract = bySN.getOilTankAllowance().subtract(entity.getQuantity());
                        tankDao.updateAllowance(entity.getTankNoSn(),subtract);


                }
            }
        } else {
            //执行修改
            entity = findBySn(sn);
            var changelog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changelog, Tl.toJson(entity), TruckAddFuelRecordEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional
    public Integer delete(String sn) {
        Integer result = dao.deleteBySn(DataState.DELETED.code,sn);
        if (result > 0){
            writeLog(ActionType.DELETE, sn, TruckAddFuelRecordEntity.class);
        }
        return result;
    }
}